﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using hedefgrup.reports.renault;
using hedefgrup.reports.renault.date;
using hedefgrup.reports.renault.database;
using System.Data;
using System.Text;

public partial class forms_salespeopleresults_salespeopleresults : reportPage
{
    private const string SQL_STATEMENT_TEMPLATE = @"
SELECT 

	SALESPERSON = SATICI,
	DEALER = BAYII,
	BASE = COUNT(*),
	S18,
	S17
	
FROM {0}

WHERE

	SATICI IS NOT NULL
	AND
	BAYII IS NOT NULL
	{1} -- MARKA LIKE 'Renault%'
	AND
	YIL * 12 + AY BETWEEN {2} AND {3} -- 24157 AND 24170
	
GROUP BY SATICI, BAYII, S18, S17

ORDER BY BAYII, SATICI ASC 
";

    private const string TABLE_ROW_TAMPLATE = @"<tr><td>{0}</td><td>{1}</td><td>{2}</td><td style='color:{3}'>{4:F}</td><td style='color:{5}'>{6:F}</td></tr>";


    protected void Page_Load(object sender, EventArgs e)
    {
        base.initialize();
        base.ensureUserIsLoggedIn();

        excelReportName.Text = base.getReportFileName();

        dataCache reportDataCache = base.getDataCache();

        reportMonth fetchEndMonth = base.getReportMonth();
        reportMonth fetchStartMonth = fetchEndMonth - (base.getCumulativeMonths() - 1);

        string dataFetchFilter = reportDataCache.dataFetchFilter;

        string sqlStatement = string.Format
        (
            SQL_STATEMENT_TEMPLATE,
            reportDataCache.dataTableName,
            string.IsNullOrEmpty(dataFetchFilter) ? "" : "AND " + reportDataCache.dataFetchFilter,
            fetchStartMonth.getMonthYearValue(),
            fetchEndMonth.getMonthYearValue()
        );

        DataTable reportData = db.query(sqlStatement, new object[] { });

        StringBuilder tableHtml = new StringBuilder();

        string currentSalesPerson = null;
        string currentDealer = null;
        int countOfS18 = 0;
        int countOfS17 = 0;
        int countOfS18_1_2 = 0;
        int countOfS17_1 = 0;

        foreach (DataRow dataRow in reportData.Rows)
        {
            if (currentSalesPerson != dataRow["SALESPERSON"].ToString())
            {
                if (currentSalesPerson != null)
                {
                    string dealerName = currentDealer;
                    if (reportDataCache.dealers.ContainsKey(currentDealer))
                    {
                        dealerName = currentDealer + " - " + reportDataCache.dealers[currentDealer].getName();
                    }
                    float s18f = (100f * countOfS18_1_2 / countOfS18);
                    float s17f = (100f * countOfS17_1 / countOfS17);
                    string s18colorCode = s18f < 91 ? "red" : "green";
                    string s17colorCode = s17f > 4 ? "red" : "green";
                    tableHtml.AppendFormat(TABLE_ROW_TAMPLATE, currentSalesPerson, dealerName, countOfS18, s18colorCode, s18f, s17colorCode, s17f);
                }

                currentSalesPerson = dataRow["SALESPERSON"].ToString();
                currentDealer = dataRow["DEALER"].ToString().Trim();
                countOfS18 = 0;
                countOfS17 = 0;
                countOfS18_1_2 = 0;
                countOfS17_1 = 0;
            }

            string s18 = dataRow["S18"].ToString();
            string s17 = dataRow["S17"].ToString();

            int count = Convert.ToInt32(dataRow["BASE"]);

            countOfS18 += count;
            countOfS17 += count;

            if (s18 == "1" || s18 == "20")
            {
                countOfS18_1_2 += count;
            }

            if (s17 == "1")
            {
                countOfS17_1 += count;
            }
        }

        reportTableHtml.Text = tableHtml.ToString();
    }
}